#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        ranges::sort(nums);
        int res = 0;
        for (int i = nums.size() - 1; i >= 0; i--)
        {
            int c = nums[i];
            int left = 0, right = i - 1;
            while (left < right)
            {
                int a = nums[left], b = nums[right];
                if (a + b > c)
                {
                    res += right - left;
                    --right;
                }
                else if (a + b <= c)
                {
                    ++left;
                }
            }
        }
        return res;
    }
};